/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.DragonPokerClient;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.Comunicator.Element;
import org.Comunicator.Message;
import org.DragonPokerClient.Thread.ClientRequest.GenericClientRequest;
import org.DragonPokerClient.Thread.ClientRequest.LoginReplyRequest;
import org.DragonPokerClient.Thread.ClientRequest.RegisterReplyRequest;
import org.DragonPokerClient.Thread.ClientRequest.RoomsReplyRequest;
import org.DragonPokerClient.graphic.Home;
import org.DragonPokerClient.graphic.Login;
import org.DragonPokerClient.graphic.prova;

/**
 *
 * @author gonçalo.margalho
 */
public class Client  extends Thread{

    private Login logingui;
    prova gui = null;
    private Home homegui;
    private String username;
    private String password;
    public static Socket s;
    public static ObjectOutputStream o;
    public static ObjectInputStream i;
    GenericClientRequest chain;
    public Client() {

        chain = new LoginReplyRequest(this);
        chain.setNext(new RegisterReplyRequest(this));
        chain.getNext().setNext(new RoomsReplyRequest(this));
    }

    @Override
    public void run() {
        try{
            System.out.println("Connetto");
        Client.s = new Socket("localhost", 1111);
        Client.o = new ObjectOutputStream(s.getOutputStream());
        Client.i = new ObjectInputStream(s.getInputStream());
        logingui = new Login();
        logingui.setVisible(true);

        //Da qui in poi gestisco le chiamate e gli invii da parte del client
        while(true){
            Thread.sleep(10);
            if (Client.s.getInputStream().available() != 0) {

                System.out.println("nuovo");
                //È arrivato un messaggio
                Object obj = Client.i.readObject();
                Message incoming = (Message) obj;
                incoming.stampa();
                chain.startProcessing(incoming);
            }
        }


        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void sendMessage(Message m){
        try {
            Client.o.writeObject(m);
            Client.o.flush();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Login getLogingui() {
        return logingui;
    }

    public void setLogingui(Login logingui) {
        this.logingui = logingui;
    }

    public Home getHomegui() {
        return homegui;
    }

    public void setHomegui(Home homegui) {
        this.homegui = homegui;
    }
    
    public Home startHome(){
        homegui = new Home();
        return homegui;
    }

}
